import { delField, getField, listField } from '@/api/modules/data/field';
import { allowEditMenu, query, table } from './data';
import { form, formRef, resetForm } from './form';
import { notify } from '@/utils/common/common';

export const create = () => {
  allowEditMenu.value = true;
  resetForm();
};
export const list = async () => {
  table.value.loading = true;
  const { data } = await listField(query.value);
  table.value.data = data.rows;
  table.value.loading = false;
  query.value.total = data.total;
};

export const edit = async (id?: string) => {
  const targetId = id ? id : table.value.keys[0];
  const { data } = await getField(`${targetId}`);
  form.value = data.data;
  allowEditMenu.value = true;
  table.value.loading = false;
};

export const remove = async (id?: string) => {
  const ids = id ? [id] : table.value.keys;
  const { data } = await delField(ids);
  notify(data);
  await list();
  table.value.loading = false;
};

export const detail = () => {
  allowEditMenu.value = true;
  table.value.loading = false;
};
export const search = () => {
  query.value.pageNum = 1;
  list();
};
